home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / admin / linuxcon.000 / linuxcon / linuxconf-1.6 / fstab / fstab2.c < prev    next >
C/C++ Source or Header  |  1996-07-25  |  9KB  |  335 lines

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <sys/stat.h>
  5. #include "../dialog/dialog.h"
  6. #include "fstab.h"
  7. #include "../paths.h"
  8. #include "../userconf/userconf.h"
  9. #include "internal.h"
  10. #include "fstab.m"
  11.  
  12. FSTAB_HELP_FILE help_mount ("mountpoint");
  13. extern FSTAB_HELP_FILE help_fstab;
  14.  
  15. /*
  16.     Return the numeric value of a SSTRING or a default value
  17. */
  18. static int fstab_get_opt_val(SSTRING &str, int defval)
  19. {
  20.     int ret = defval;
  21.     if (!str.is_empty()){
  22.         ret = str.getval();
  23.     }
  24.     return ret;
  25. }
  26.  
  27. static int fstab_rootaccess()
  28. {
  29.     return perm_rootaccess("to maintain volumes");
  30. }
  31.  
  32. /*
  33.     Edit one entry of the /etc/fstab file.
  34.     Return 0 if the user accepted the changes
  35.     Return -1 if the user escape the dialog
  36.     Return 1 if the user want to delete this entry
  37. */
  38. PUBLIC int FSTAB_ENTRY::edit(FSTAB_ENTRY_TYPE fstype)
  39. {
  40.     DIALOG dia;
  41.     char server[100];
  42.     char volume[100];
  43.     if (fstype == FSTAB_ENTRY_NFS){
  44.         source.copy (server);
  45.         char *pt = strchr(server,':');
  46.         volume[0] = '\0';
  47.         if (pt != NULL){
  48.             *pt++ = '\0';
  49.             strcpy (volume,pt);
  50.         }            
  51.         dia.newf_str (MSG_U(F_SERVER,"Server"),server,sizeof(server)-1);
  52.         dia.newf_str (MSG_U(F_VOLUME,"Volume"),volume,sizeof(volume)-1);
  53.         type.setfrom ("nfs");
  54.     }else if (fstype == FSTAB_ENTRY_SAMBA){
  55.         char buf[200];
  56.         source.copy (buf);
  57.         char *pt = buf;
  58.         while (*pt == '/') pt++;
  59.         strcpy (server,pt);
  60.         pt = strchr (server,'/');
  61.         volume[0] = '\0';
  62.         if (pt != NULL){
  63.             *pt++ = '\0';
  64.             strcpy (volume,pt);
  65.         }            
  66.         dia.newf_str (MSG_R(F_SERVER),server,sizeof(server)-1);
  67.         dia.newf_str (MSG_U(F_SHARE,"Share"),volume,sizeof(volume)-1);
  68.         type.setfrom ("smbfs");
  69.     }else{
  70.         FIELD_COMBO *co = dia.newf_combo(MSG_U(F_PARTITION,"Partition")
  71.             ,source);
  72.         PARTITIONS *parts = partition_load();
  73.         for (int i=0; i<parts->getnb(); i++){
  74.             PARTITION *p = parts->getitem(i);
  75.             char str[80];
  76.             p->formatinfo (str);
  77.             if (fstype == FSTAB_ENTRY_SWAP){
  78.                 if (p->isswap()) co->addopt(p->getdevice(),str);
  79.             }else{
  80.                 if (p->isdos() || p->islinux()){
  81.                     co->addopt(p->getdevice(),str);
  82.                 }
  83.             }
  84.         }
  85.         if (fstype == FSTAB_ENTRY_SWAP){
  86.             type.setfrom ("swap");
  87.             mpoint.setfrom ("none");
  88.         }else{
  89.             FIELD_COMBO *cot = dia.newf_combo(MSG_U(F_TYPE,"Type"),type);
  90.             cot->addopt("ext2",MSG_U(F_EXT2,"Standard Linux"));
  91.             cot->addopt("minix",MSG_U(F_MINIX,"Sometime for floppy"));
  92.             cot->addopt("msdos"," ");
  93.             cot->addopt("umsdos",MSG_U(F_UMSDOS,"Superset of msdos"));
  94.             cot->addopt("hpfs",MSG_U(F_OS2,"OS/2 High Performance FS"));
  95.             cot->addopt("sysv",MSG_U(F_UNIXV,"Unix system V old fs"));
  96.             cot->addopt("xiafs",MSG_U(F_XIAFS,"Old linux fs"));
  97.         }
  98.     }
  99.     if (fstype != FSTAB_ENTRY_SWAP){
  100.         dia.newf_str (MSG_U(F_MPOINT,"Mount point"),mpoint);
  101.         dia.newf_title ("",MSG_U(T_GENOPTIONS,"General options"));
  102.         dia.newf_chk (MSG_U(F_OPTIONS,"Options"),bool_opt.readonly
  103.             ,MSG_U(F_READONLY,"Read only"));
  104.         dia.newf_chk ("",bool_opt.user,MSG_U(F_USERMNT,"User mountable"));
  105.         dia.newf_chk ("",bool_opt.noauto,MSG_U(F_BOOTTIME,"Not mount at boot time"));
  106.         dia.newf_chk ("",bool_opt.noexec,MSG_U(F_NOEXEC,"No program allowed to execute"));
  107.         dia.newf_chk ("",bool_opt.nodev,MSG_U(F_NOSPC,"No special device file support"));
  108.         dia.newf_chk ("",bool_opt.nosuid,MSG_U(F_NOSUID,"No setuid programs allowed"));
  109.     }
  110.     SSTRING msdos_uid,msdos_gid,msdos_umask;
  111.     SSTRING nfs_rsize,nfs_wsize;
  112.     GROUPS groups;
  113.     groups.sortbyname();
  114.     USERS users;
  115.     users.sortbyname();
  116.     if (fstype == FSTAB_ENTRY_LOCAL){
  117.         dia.newf_num (MSG_U(F_DUMPFREQ,"Dump frequency"),dumpfreq);
  118.         dia.newf_num (MSG_U(F_CHKPRI,"Fsck priority"),fsckpriority);
  119.         dia.newf_title ("",MSG_U(T_MSOPT,"(U)MsDOS and HPFS options")); 
  120.         USER *user = users.getfromuid(msdos_opt.uid);
  121.         if (user != NULL){
  122.             msdos_uid.setfrom(user->getname());
  123.         }else if (msdos_opt.uid != MSDOS_OPT_UNUSE){
  124.             msdos_uid.setfrom(msdos_opt.uid);
  125.         }
  126.         FIELD_COMBO *cv = dia.newf_combo (MSG_U(F_DEFUID,"default user id")
  127.             ,msdos_uid);
  128.         int i;
  129.         for (i=0; i<users.getnb(); i++){
  130.             user = users.getitem(i);
  131.             cv->addopt(user->getname(),user->getgecos());
  132.         }
  133.         GROUP *grp = groups.getfromgid(msdos_opt.gid);
  134.         if (grp != NULL){
  135.             msdos_gid.setfrom(grp->getname());
  136.         }else if(msdos_opt.gid != MSDOS_OPT_UNUSE){
  137.             msdos_gid.setfrom(msdos_opt.gid);
  138.         }
  139.         cv = dia.newf_combo (MSG_U(F_DEFGID,"default group id"), msdos_gid);
  140.         for (i=0; i<groups.getnb(); i++){
  141.             grp = groups.getitem(i);
  142.             cv->addopt(grp->getname());
  143.         }
  144.         if (msdos_opt.perm != MSDOS_OPT_UNUSE){
  145.             msdos_umask.setfrom (msdos_opt.perm);
  146.         }
  147.         dia.newf_str (MSG_U(F_DEFPERM,"default permission") , msdos_umask);
  148.         FIELD_COMBO *conv = dia.newf_combo (
  149.             MSG_U(F_TRAMODE,"translation mode")
  150.             ,msdos_opt.conv);
  151.         conv->addopt ("binary",MSG_U(F_NOTRA,"No file translation"));
  152.         conv->addopt ("auto",MSG_U(F_EXTTRA,"Translate based on extension"));
  153.         conv->addopt ("text",MSG_U(F_ALWTRANS,"Always translate"));
  154.     }else if (fstype == FSTAB_ENTRY_NFS){
  155.         dia.newf_title ("",MSG_U(T_NFSOPT,"NFS options")); 
  156.         dia.newf_chk ("",nfs_opt.soft,MSG_U(F_SOFTMNT,"Soft mount"));
  157.         dia.newf_chk ("",nfs_opt.bg,MSG_U(F_BGMOUNT,"Background mount"));
  158.         if (nfs_opt.rsize != NFS_OPT_UNUSE){
  159.             nfs_rsize.setfrom(nfs_opt.rsize);
  160.         }
  161.         dia.newf_str (MSG_U(F_READSIZE,"read size"),nfs_rsize);
  162.         if (nfs_opt.wsize != NFS_OPT_UNUSE){
  163.             nfs_wsize.setfrom(nfs_opt.wsize);
  164.         }
  165.         dia.newf_str ("write size",nfs_wsize);
  166.     }
  167.     dia.newf_title ("","");
  168.     dia.newf_str(MSG_U(F_OTHEROPT,"Other options"),options);
  169.     dia.newf_str(MSG_U(F_COMMENT,"Comment"),comment);
  170.     int ret = -1;
  171.     int nof = 0;
  172.     while (1){
  173.         MENU_STATUS code = dia.edit(
  174.             MSG_U(T_VOLSPEC,"Volume specification")
  175.             ,MSG_U(I_VOLSPEC,"You must enter the specification of a volume\n"
  176.              "or partition and the position (mount point)\n"
  177.              "where you want to install this volume\n"
  178.              "in the directory structure of this workstation\n")
  179.             ,help_mount.getpath()
  180.             ,nof,MENUBUT_CANCEL|MENUBUT_ACCEPT|MENUBUT_DEL);
  181.         if (code == MENU_CANCEL || code == MENU_ESCAPE){
  182.             break;
  183.         }else if (code == MENU_DEL){
  184.             if (xconf_areyousure(MSG_U(Q_DELENTRY
  185.                 ,"Confirm deletion of /etc/fstab entry"))){
  186.                 ret = 1;
  187.                 break;
  188.             }
  189.         }else if (code==MENU_ACCEPT){
  190.             if (!fstab_rootaccess()) continue;
  191.             if (fstype == FSTAB_ENTRY_NFS){
  192.                 char buf[200];
  193.                 sprintf (buf,"%s:%s",server,volume);
  194.                 source.setfrom (buf);
  195.                 nfs_opt.rsize = fstab_get_opt_val(nfs_rsize
  196.                     ,NFS_OPT_UNUSE);
  197.                 nfs_opt.wsize = fstab_get_opt_val(nfs_wsize
  198.                     ,NFS_OPT_UNUSE);
  199.             }else if (fstype == FSTAB_ENTRY_SAMBA){
  200.                 char buf[200];
  201.                 sprintf (buf,"//%s/%s",server,volume);
  202.                 source.setfrom (buf);
  203.             }else{
  204.                 GROUP *grp = groups.getitem(msdos_gid.get());
  205.                 if (grp != NULL){
  206.                     msdos_opt.gid = grp->getgid();
  207.                 }else{
  208.                     msdos_opt.gid = fstab_get_opt_val(
  209.                         msdos_gid,MSDOS_OPT_UNUSE);
  210.                 }
  211.                 USER *usr = users.getitem(msdos_uid.get());
  212.                 if (usr != NULL){
  213.                     msdos_opt.uid = usr->getuid();
  214.                 }else{
  215.                     msdos_opt.uid = fstab_get_opt_val(
  216.                         msdos_uid,MSDOS_OPT_UNUSE);
  217.                 }
  218.                 msdos_opt.perm = fstab_get_opt_val(
  219.                         msdos_umask,MSDOS_OPT_UNUSE);
  220.             }
  221.             valid = 1;
  222.             if (check() == 0){
  223.                 ret = 0;
  224.                 break;
  225.             }
  226.         }
  227.     }
  228.     if (ret != 0) dia.restore();
  229.     return ret;
  230. }
  231.  
  232. /*
  233.     Edit, add, delete entry of /etc/fstab.
  234.     seltype: 0 local partition
  235.          1 Remote volume
  236.          2 swap
  237. */
  238. PUBLIC void FSTAB::edit (FSTAB_ENTRY_TYPE fstype)
  239. {
  240.     int choice = 0;
  241.     while (1){
  242.         int nb = getnb();
  243.         const char *tb[100];
  244.         static const char *tbfs[]={
  245.             MSG_U(T_LOCALVOL,"Local volume"),
  246.             MSG_U(T_NFSVOL,"NFS volume"),
  247.             MSG_U(T_SMBVOL,"SMB volume"),
  248.             MSG_U(T_SWAPSPACE,"Swap space"),
  249.             MSG_U(T_NOVELL,"NOVELL volume")
  250.         };
  251.         int i;
  252.         int maxlen1 = 0;
  253.         int maxlen2 = 0;
  254.         int nbshow = 0;
  255.         FSTAB_ENTRY *tbsel[50];
  256.         // Collect viewable entries and get format information
  257.         for (i=0; i<nb; i++){
  258.             FSTAB_ENTRY *ent = getitem(i);
  259.             if (ent->is_valid()
  260.                 && ent->gettype() == fstype){
  261.                 int len1 = strlen(ent->getmpoint());
  262.                 int len2 = strlen(ent->getfs());
  263.                 if (len1 > maxlen1) maxlen1 = len1;
  264.                 if (len2 > maxlen2) maxlen2 = len2;
  265.                 tbsel[nbshow++] = ent;
  266.             }
  267.         }
  268.         int j=0;
  269.         PARTITIONS *parts = partition_load();
  270.         for (i=0; i<nbshow; i++){
  271.             FSTAB_ENTRY *ent = tbsel[i];
  272.             const char *source = ent->getsource();
  273.             tb[j++] = source;
  274.             char buf[100];
  275.             PARTITION *p = parts->getitem(source);
  276.             char str[80];
  277.             str[0] = '\0';
  278.             struct stat s;
  279.             if (p != NULL){
  280.                 p->formatinfo (str);
  281.             }else if (stat(source,&s)!=-1){
  282.                 sprintf (str,"%7ldM",s.st_size/(1024*1024l));
  283.             }
  284.             sprintf (buf,"%-*s %-*s %s"
  285.                 ,maxlen1,ent->getmpoint()
  286.                 ,maxlen2,ent->getfs()
  287.                 ,str);
  288.             tb[j++] = strdup (buf);
  289.         }
  290.         tb[j] = NULL;
  291.         MENU_STATUS code = xconf_menu (
  292.             tbfs[fstype] 
  293.             ,MSG_U(I_MOUNTS,"You can edit, add, or delete mounts")
  294.             ,help_fstab
  295.             ,NULL
  296.             ,NULL
  297.             ,NULL
  298.             ,MSG_U(I_ADDDEF,"to add a new definition")
  299.             ,tb,choice);
  300.         for (i=0; i<nbshow; i++){
  301.             free ((char*)tb[i*2+1]);
  302.         }
  303.         if (code == MENU_ESCAPE || code == MENU_QUIT){
  304.             break;
  305.         }else if (code == MENU_OK){
  306.             FSTAB_ENTRY *ent = tbsel[choice];
  307.             int ok = ent->edit(fstype);
  308.             if (ok != -1){
  309.                 if (ok == 1) remove_del (ent);
  310.                 write();
  311.             }
  312.         }else if (fstab_rootaccess()){
  313.             if (code == MENU_ADD){
  314.                 FSTAB_ENTRY *ent = new FSTAB_ENTRY;
  315.                 if (ent->edit(fstype)==0){
  316.                     add (ent);
  317.                     write();
  318.                 }else{
  319.                     delete ent;
  320.                 }
  321.             }
  322.         }
  323.     }
  324. }
  325.  
  326. /*
  327.     Edit, add, delete entry of /etc/fstab
  328. */
  329. void fstab_edit (FSTAB_ENTRY_TYPE fstype)
  330. {
  331.     FSTAB fs;
  332.     fs.edit (fstype);
  333. }
  334.  
  335.